public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null) {
            return list;
        }
        LinkedList<TreeNode> stack = new LinkedList<>();
        TreeNode p;
        stack.push(root);
        while(!stack.isEmpty()) {
            p = stack.pop();
            list.add(p.val);
            if(p.right != null)
                stack.push(p.right);
            if(p.left != null)
                stack.push(p.left);
        }
        return list;
    }
}